package com.tanhua.sso.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.sso.mapper.UserMapper;
import com.tanhua.sso.pojo.User;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Value("${jwt.secret}")
    private String secret;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public String login(String phone, String code) {
        String redisKey = "CHECK_CODE"+phone;
        boolean isNew = false;
        String redisData = redisTemplate.opsForValue().get(redisKey);

        if(!StringUtils.equals(code,redisData)){
            return null;//验证码错误
        }
        //验证码校验完成后删除
        this.redisTemplate.delete(redisKey);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",phone );

        User user = this.userMapper.selectOne(queryWrapper);

        if(null == user){
            user = new User();
            user.setMobile(phone);
            user.setPassword(DigestUtils.md5Hex("123456"));

            this.userMapper.insert(user);
            isNew = true;
        }

        //生成token
        Map<String, Object> claims = new HashMap<String, Object>();
        claims.put("id", user.getId());

        // 生成token
        String token = Jwts.builder()
                .setClaims(claims) //payload，存放数据的位置，不能放置敏感数据，如：密码等
                .signWith(SignatureAlgorithm.HS256, secret) //设置加密方法和加密盐
                .setExpiration(new DateTime().plusHours(12).toDate()) //设置过期时间，12小时后过期
                .compact();

        //发送用户登录成功消息
        try {
            Map<String,Object> msg = new HashMap<>();
            msg.put("id",user.getId());
            msg.put("date",System.currentTimeMillis());
            //this.rocketMQTemplate.convertAndSend("tanhua-sso-login");
        } catch (MessagingException e) {
            log.error("mq发送消息失败！");
        }


        return token + "|" + isNew;
    }

    public User queryUserByToken(String token) {
        try {
            // 通过token解析数据
            Map<String, Object> body = Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
            System.out.println(body); //{mobile=1333333333, id=2, exp=1605513392}

            User user = new User();
            user.setId(Long.valueOf(body.get("id").toString()));
            //需要返回对象中的mobile 查询数据库获得mobile
            //为了避免每次查询数据库导致性能过低，将手机号存入缓存
            //数据缓存需要设置过期时间，过期时间与token时间一致
            String redisKey = "XY_TANHUA_USER_MOBILE"+ user.getId();
            if(this.redisTemplate.hasKey(redisKey)){
                String mobile = this.redisTemplate.opsForValue().get(redisKey);
                user.setMobile(mobile);
            }else {
                // 查询数据库
                User u = this.userMapper.selectById(user.getId());
                user.setMobile(u.getMobile());
                //手机号写入缓存
                long timeout = Long.valueOf(body.get("exp").toString()) * 1000 - System.currentTimeMillis();
                this.redisTemplate.opsForValue().set(redisKey,u.getMobile(),timeout, TimeUnit.MILLISECONDS);
            }
            return user;
        } catch (ExpiredJwtException e) {
            log.info("token已经过期！token = " + token);
        } catch (Exception e) {
            log.error("token不合法！token = "+ token,e);
        }
        return null;
    }
}
